package com.africanmoviesmobile.ui;

import java.io.IOException;
import java.util.Hashtable;

import org.pixmob.httpclient.HttpClientException;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.africanmoviesmobile.R;
import com.africanmoviesmobile.webservice.AmmWebService;
import com.africanmoviesmobile.webservice.BasicAsyncCallback;
import com.africanmoviesmobile.webservice.WebServiceException;
import com.africanmoviesmobile.webservice.inf.User;
import com.africanmoviesmobile.webservice.inf.WebserviceObject;

public class RequestMovieActivity extends BaseActivity{

	private ProgressDialog mProgressDialog;
	
	private EditText mMovieTitle;
	private EditText mReleaseYear;
	private Spinner mCountry;
	private Button mSubmitRequest;
	
	AmmWebService mAmmWebService;
	
	private String mSelectedCountry;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.request_movie_activity);

		mMovieTitle = (EditText) findViewById(R.id.movie_title);
		mReleaseYear = (EditText) findViewById(R.id.release_year);
		mCountry = (Spinner) findViewById(R.id.country);
		mSubmitRequest = (Button) findViewById(R.id.submit_request);
		mSubmitRequest.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View view) {
				submitRequest();
			}
		});
		
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.africa_countries_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCountry.setAdapter(adapter);
		mCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
				mSelectedCountry = (String) parent.getItemAtPosition(pos);
		    }
		    public void onNothingSelected(AdapterView<?> parent) {
		    	mSelectedCountry = (String) parent.getSelectedItem();
		    }
		});
		
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage("Please wait...");

		mAmmWebService = AmmWebService.getInstance(this);		
	}
	
	private void submitRequest() {
				
		if(mMovieTitle.length() == 0){
			alertMessage(R.string.movie_title_required);
		}else if(mReleaseYear.length() == 0){
			alertMessage(R.string.release_year_required);
		}else{

			mProgressDialog.show();
			
			mAmmWebService.getUser(getLoginEmail(), new BasicAsyncCallback() {

				public void onComplete(final WebserviceObject[] result, final Object state) {

					User user = (User) result[0];
					
					if(user != null){
												
						Hashtable<String,String> data = new Hashtable<String,String>();
						data.put("movieTitle", mMovieTitle.getText().toString());
						data.put("releaseYear", mReleaseYear.getText().toString());
						data.put("countryName", mSelectedCountry);
						
						boolean response = false;
						
						try {
							response = user.addRequest(data);
						} catch (HttpClientException e) {
							Log.w("AMM HttpClientException:", e.getMessage());
						} catch (IOException e) {
							Log.w("AMM IOException:", e.getMessage());
						} catch (WebServiceException e) {
							Log.w("AMM WebServiceException:", e.getMessage());
							toastMessage(e.getMessage());
						}
						
						runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
						
						if(response){
							
							runOnUiThread(new Runnable() {public void run() { 
								
								new AlertDialog.Builder(RequestMovieActivity.this)
			                    .setMessage(R.string.request_sent)
			                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int which) {
										startActivity(new Intent(RequestMovieActivity.this, RequestsActivity.class));
									}
								})
								.setCancelable(false)
			                    .show();
								
							}});
							
						}else{
							toastMessage(R.string.request_movie_error);
						}
						
						
					}
				}

				public void onHttpClientException(final HttpClientException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM HttpClientException:", e.getMessage());
				}
				
				public void onIOException(final IOException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM IOException:", e.getMessage());
				}

				public void onWebServiceException(final WebServiceException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM WebServiceException:", e.getMessage());
					toastMessage(R.string.user_info_error);
				}

			}, null);

		}
		
	}
	
}
